﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonLib;
using RibbonLib.Controls;
using RibbonLib.Controls.Events;
using RibbonLib.Interop;
using GlxRRSMain.UserControls;
using GlxRRSDB;
using System.Configuration;
using GlxRRSReporter.Utils;
using GlxRRSMain.Utils;


namespace GlxRRSMain
{
    public enum RibbonMarkupCommands : uint
    {
        cmdTabDataManagement = 1001,
        cmdGroupData = 1002,
        cmdManageCinema = 1003,
        cmdManageFilm = 1004,
        cmdManageTarget = 1005,
        cmdManageFilmRevenue = 1006,
        cmdManageBORevenue = 1007,
        cmdManageCORevenue = 1008,
        cmdManageSession = 1009,
        cmdManageTicket = 1011,
        cmdManageCinemaTarget = 1012,
        //
        cmdHelpButton = 1010,
        //
        cmdTabReportManagement = 1021,
        cmdGroupReport = 1022,
        cmdDailyRevenueReport = 1023,
        cmdMonthlyReportByCinema = 1024,
        cmdTicketReportByCinema = 1025,
        cmdWeeklyReportByCinema = 1026,
        cmdWeeklyReportByFilm = 1027,
        //
        cmdTabImportData = 2001,
        cmdGroupImport = 2002,
        cmdImportBOData = 2003,
        cmdImportCOData = 2004,
        cmdImportSessionData = 2005,
        cmdImportDailyData = 2006,
        cmdImportFilmRevenueData = 2007,
        cmdImportBOEx = 2008,
        //
        cmdMenuExit = 1041,
        //
        cmdConfig = 2001
    }
    public partial class Form1 : Form
    {
        Dictionary<RibbonMarkupCommands, TabPages.TabPage> x_ListCurrentTabPages = new Dictionary<RibbonMarkupCommands, TabPages.TabPage>();
        // tab data management
        private RibbonButton x_rbtnManageCinema;
        private RibbonButton x_rbtnManageFilm;
        private RibbonButton x_rbtnManageTarget;
        private RibbonButton x_rbtnManageCinemaTarget;
        private RibbonButton x_rbtnManageFilmRevenue;
        private RibbonButton x_rbtnManageBORevenue;
        private RibbonButton x_rbtnManageCORevenue;
        private RibbonButton x_rbtnManageSession;
        private RibbonButton x_rbtnManageTicket;
        // tab report
        private RibbonButton x_rbtnDailyRevenueReport;
        private RibbonButton x_rbtnMonthlyReportByCinema;
        private RibbonButton x_rbtnTicketReportByCinema;
        private RibbonButton x_rbtnWeeklyReportByCinema;
        private RibbonButton x_rbtnWeeklyReportByFilm;
        // tab import
        private RibbonButton x_rbtnImportBOData;
        private RibbonButton x_rbtnImportCOData;
        private RibbonButton x_rbtnImportSessionData;
        private RibbonButton x_rbtnImportDailyData;
        private RibbonButton x_rbtnImportFilmRevenueData;
        private RibbonButton x_rbtnImportBOEx;
        //
        private bool isReady = false;
        public Form1()
        {
            InitializeComponent();
            // tab data management
            initTabDataManagement();
            // tab report
            initTabReportManagement();
            // tab import
            initTabImportData();
        }
        private void initTabDataManagement()
        {
            // initialize
            x_rbtnManageCinema = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageCinema);
            x_rbtnManageFilm = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageFilm);
            x_rbtnManageTarget = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageTarget);
            x_rbtnManageCinemaTarget = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageCinemaTarget);
            x_rbtnManageFilmRevenue = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageFilmRevenue);
            x_rbtnManageBORevenue = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageBORevenue);
            x_rbtnManageCORevenue = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageCORevenue);
            x_rbtnManageSession = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageSession);
            x_rbtnManageTicket = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdManageTicket);
            // assign EventHandler
            x_rbtnManageCinema.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageCinema_ExecuteEvent);
            x_rbtnManageFilm.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageFilm_ExecuteEvent);
            x_rbtnManageTarget.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageTarget_ExecuteEvent);
            x_rbtnManageCinemaTarget.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageCinemaTarget_ExecuteEvent);
            x_rbtnManageFilmRevenue.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageFilmRevenue_ExecuteEvent);
            x_rbtnManageBORevenue.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageBORevenue_ExecuteEvent);
            x_rbtnManageCORevenue.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageCORevenue_ExecuteEvent);
            x_rbtnManageSession.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageSession_ExecuteEvent);
            x_rbtnManageTicket.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdManageTicket_ExecuteEvent);
        }

        private void initTabReportManagement()
        {
            x_rbtnDailyRevenueReport = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdDailyRevenueReport);
            x_rbtnMonthlyReportByCinema = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdMonthlyReportByCinema);
            x_rbtnTicketReportByCinema = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdTicketReportByCinema);
            x_rbtnWeeklyReportByCinema = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdWeeklyReportByCinema);
            x_rbtnWeeklyReportByFilm = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdWeeklyReportByFilm);

            //
            x_rbtnDailyRevenueReport.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdDailyRevenueReport_ExecuteEvent);
            x_rbtnMonthlyReportByCinema.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdMonthlyReportByCinema_ExecuteEvent);
            x_rbtnTicketReportByCinema.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdTicketReportByCinema_ExecuteEvent);
            x_rbtnWeeklyReportByCinema.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdWeeklyReportByCinema_ExecuteEvent);
            x_rbtnWeeklyReportByFilm.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdWeeklyReportByFilm_ExecuteEvent);
        }

        private void initTabImportData()
        {
            x_rbtnImportBOData = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportBOData);
            x_rbtnImportCOData = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportCOData);
            x_rbtnImportSessionData = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportSessionData);
            x_rbtnImportDailyData = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportDailyData);
            x_rbtnImportFilmRevenueData = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportFilmRevenueData);
            x_rbtnImportBOEx = new RibbonButton(x_ribbon, (uint)RibbonMarkupCommands.cmdImportBOEx);

            //
            x_rbtnImportBOData.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportBOData_ExecuteEvent);
            x_rbtnImportCOData.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportCOData_ExecuteEvent);
            x_rbtnImportSessionData.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportSessionData_ExecuteEvent);
            x_rbtnImportDailyData.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportDailyData_ExecuteEvent);
            x_rbtnImportFilmRevenueData.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportFilmRevenueData_ExecuteEvent);
            x_rbtnImportBOEx.ExecuteEvent += new EventHandler<ExecuteEventArgs>(cmdImportBOEx_ExecuteEvent);
        }
        private void addTab(String tabTitle, RibbonMarkupCommands id, Control ctrl, String tabTooltip = null)
        {
            if (!isReady)
            {
                DialogResult rc = MessageBox.Show("Connection string fail. Do you want to correct it?", "", MessageBoxButtons.YesNo);
                if (rc == DialogResult.Yes)
                {
                    ConfigurationForm form = new ConfigurationForm();
                    form.ShowDialog();
                    isReady = form.isReady;
                }
                return;
            }
            if (x_ListCurrentTabPages.ContainsKey(id))
            {
                TabPages.TabPage t = x_ListCurrentTabPages[id];
                x_pageCollection.CurrentPage = t;
            }
            else
            {
                TabPages.TabPage t = new TabPages.TabPage(tabTitle, ctrl, tabTooltip);
                x_pageCollection.Add(t);
                x_ListCurrentTabPages.Add(id, t); x_pageCollection.CurrentPage = t;
            }
        }
        private void addTab(object sender, RibbonMarkupCommands id, Control ctrl, String tabTooltip = null)
        {
            RibbonButton rctrl = (RibbonButton)sender;
            String tabTitle = rctrl.LabelDescription;
            addTab(tabTitle, id, ctrl, tabTooltip);
        }
        private void cmdManageCinema_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            // Close form asynchronously since we are in a ribbon event 
            // handler, so the ribbon is still in use, and calling Close 
            // will eventually call _ribbon.DestroyFramework(), which is 
            // a big no-no, if you still use the ribbon.
            //this.BeginInvoke(new MethodInvoker(this.Close));
            CinemaManagementControl cinema = new CinemaManagementControl();
            addTab("Cinema Management", RibbonMarkupCommands.cmdManageCinema, cinema, "Mange cinema data");
        }
        private void cmdManageFilm_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            FilmTargetManagementControl film = new FilmTargetManagementControl();
            addTab("Film Management", RibbonMarkupCommands.cmdManageFilm, film);
        }
        private void cmdManageTarget_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            TargetManagementControl ctrl = new TargetManagementControl();
            addTab("Film Target Management", RibbonMarkupCommands.cmdManageTarget, ctrl);
        }
        private void cmdManageCinemaTarget_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            CinemaTargetManagementControl ctrl = new CinemaTargetManagementControl();
            addTab("Cinema Target Management", RibbonMarkupCommands.cmdManageCinemaTarget, ctrl);
        }
        private void cmdManageFilmRevenue_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            FilmRevenueManagementControl ctrl = new FilmRevenueManagementControl();
            addTab("Film Revenue Management", RibbonMarkupCommands.cmdManageFilmRevenue, ctrl);
        }
        private void cmdManageBORevenue_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            BORevenueManagementControl ctrl = new BORevenueManagementControl();
            addTab("BO Revenue Management", RibbonMarkupCommands.cmdManageBORevenue, ctrl);
        }
        private void cmdManageCORevenue_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            CORevenueManagementControl ctrl = new CORevenueManagementControl();
            addTab("CO Revenue Management", RibbonMarkupCommands.cmdManageCORevenue, ctrl);
        }
        private void cmdManageSession_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            SessionManagementControl ctrl = new SessionManagementControl();
            addTab("Session Management", RibbonMarkupCommands.cmdManageSession, ctrl);
        }
        private void cmdManageTicket_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            TicketManagementControl ctrl = new TicketManagementControl();
            addTab("Ticket Management", RibbonMarkupCommands.cmdManageTicket, ctrl);
        }
        private void cmdDailyRevenueReport_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            DailyRevenueReportControl ctrl = new DailyRevenueReportControl();
            addTab("Daily Report Revenue Report", RibbonMarkupCommands.cmdDailyRevenueReport, ctrl);
        }
        private void cmdMonthlyReportByCinema_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            MonthlyReportByCinemaControl ctrl = new MonthlyReportByCinemaControl();
            addTab("Monthly Report By Cinema", RibbonMarkupCommands.cmdMonthlyReportByCinema, ctrl);
        }
        public void cmdTicketReportByCinema_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            TicketReportByCinemaControl ctrl = new TicketReportByCinemaControl();
            addTab("Ticket Report By Cinema", RibbonMarkupCommands.cmdTicketReportByCinema, ctrl);
        }

        public void cmdWeeklyReportByCinema_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            WeeklyReportByCinemaControl ctrl = new WeeklyReportByCinemaControl();
            addTab("Weekly Report By Cinema", RibbonMarkupCommands.cmdWeeklyReportByCinema, ctrl);
        }

        public void cmdWeeklyReportByFilm_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            WeeklyReportByFilmControl ctrl = new WeeklyReportByFilmControl();
            addTab("Weekly Report By Film", RibbonMarkupCommands.cmdWeeklyReportByFilm, ctrl);
        }

        public void cmdImportBOData_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportBODataControl ctrl = new ImportBODataControl();
            addTab("Import BO Data", RibbonMarkupCommands.cmdImportBOData, ctrl);
        }

        public void cmdImportCOData_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportCODataControl ctrl = new ImportCODataControl();
            addTab("Import CO Data", RibbonMarkupCommands.cmdImportCOData, ctrl);
        }

        public void cmdImportSessionData_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportSessionDataControl ctrl = new ImportSessionDataControl();
            addTab("Import Session Data", RibbonMarkupCommands.cmdImportSessionData, ctrl);
        }

        public void cmdImportDailyData_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportDailyDataControl ctrl = new ImportDailyDataControl();
            addTab("Import Daily Data", RibbonMarkupCommands.cmdImportDailyData, ctrl);
        }

        public void cmdImportFilmRevenueData_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportFilmRevenueDataControl ctrl = new ImportFilmRevenueDataControl();
            addTab("Import Film Revenue Data", RibbonMarkupCommands.cmdImportFilmRevenueData, ctrl);
        }

        public void cmdImportBOEx_ExecuteEvent(object sender, ExecuteEventArgs e)
        {
            ImportBOExDataControl ctrl = new ImportBOExDataControl();
            addTab("Import BO Data Ex", RibbonMarkupCommands.cmdImportBOEx, ctrl);
        }

        private void x_pageCollection_PageRemoved(TabPages.TabPage page)
        {
            if (x_ListCurrentTabPages.ContainsValue(page))
            {
                // or you could modify state
                List<RibbonMarkupCommands> keys = x_ListCurrentTabPages
                  .Where(kvp => kvp.Value == page)
                  .Select(kvp => kvp.Key)
                  .ToList();

                foreach (RibbonMarkupCommands theKey in keys)
                {
                    x_ListCurrentTabPages.Remove(theKey);
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string startupPath = System.IO.Directory.GetCurrentDirectory();
            //string startupPath = Environment.CurrentDirectory; 
            //MessageBox.Show(startupPath);
            //
            //Map the roaming configuration file. This enables the application to access the configuration file using the 
            //System.Configuration.Configuration class 
            try
            {
                FileHelper.cleanExportData();
            }
            catch (Exception ex)
            {
            }
            //
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            //
            configFileMap.ExeConfigFilename = "db.config";
            // Get the mapped configuration file. 
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            string connectionString = config.ConnectionStrings.ConnectionStrings["ReportDB"].ConnectionString;
            bool rc = ReportDataSet.init(connectionString);
            if (!rc)
            {
                isReady = false;
                ConfigurationForm form = new ConfigurationForm();
                form.ShowDialog();
                isReady = form.isReady;
            }
            else
            {
                isReady = true;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            ExportToExcel.quitExcel();
        }
    }
}
